/**
 * 
 */
package dt.inlee.system.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import dt.inlee.common.dao.impl.BaseDao;
import dt.inlee.common.exception.FrameworkException;
import dt.inlee.common.util.BaseUtil;
import dt.inlee.domain.SysRole;

/**
 * @功能说明：角色数据访问层
 * @作者： 印鲜刚
 * @创建日期： 2010-5-6 @
 */
@Repository("sysRoleDao")
public class SysRoleDao extends BaseDao {
	/**
	 * 根据条件查询角色
	 * 
	 * @param name
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<SysRole> findAllSysRoles(String name, int currentPage,
			int pageSize, int wyId, List<SysRole> roleList)
			throws FrameworkException {
		Criteria criteria = hibernateTemplate.getSessionFactory()
				.getCurrentSession().createCriteria(SysRole.class);
		if (!BaseUtil.isEmpty(name)) {
			criteria.add(Restrictions.eq("name", name));

		}
		List<String> list = new ArrayList<String>();
		for (SysRole sysRole : roleList) {
			list.add(sysRole.getCode());
		}

		boolean flag = list.contains("manage");

		if (!flag) {
			criteria.add(Restrictions.eq("wyId", wyId));
		}

		if (currentPage > 0 && pageSize > 0) {
			criteria.setFirstResult((currentPage - 1) * pageSize);
			criteria.setMaxResults(pageSize);
		}
		return criteria.list();
	}

	/**
	 * 根据条件enterpriseId查询角色
	 * 
	 * @param name
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<SysRole> getListSysRoles(String enterpriseId)
			throws FrameworkException {
		Criteria criteria = hibernateTemplate.getSessionFactory()
				.getCurrentSession().createCriteria(SysRole.class);
		criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		return criteria.list();
	}

	/**
	 * 根据条件统计角色总数
	 * 
	 * @param name
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public int getSysRoleTotalRows(String name) throws FrameworkException {
		Criteria criteria = hibernateTemplate.getSessionFactory()
				.getCurrentSession().createCriteria(SysRole.class);
		if (!BaseUtil.isEmpty(name)) {
			criteria.add(Restrictions.eq("name", name));
		}
		criteria.setProjection(Projections.countDistinct("id"));
		List<Integer> count = criteria.list();
		return count.get(0).intValue();
	}

	/**
	 * 通过角色名字查找角色
	 * 
	 * @param name
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public SysRole findSysRoleByName(String name, int wyId)
			throws FrameworkException {
		Criteria criteria = hibernateTemplate.getSessionFactory()
				.getCurrentSession().createCriteria(SysRole.class);
		if (!BaseUtil.isEmpty(name)) {
			criteria.add(Restrictions.eq("name", name));
		}
		if (!BaseUtil.isEmpty(wyId) && wyId != 0) {
			criteria.add(Restrictions.eq("wyId", wyId));
		}
		List<SysRole> sysRoles = criteria.list();
		if (!BaseUtil.isEmpty(sysRoles) && sysRoles.size() > 0)
			return sysRoles.get(0);
		return null;
	}

	/**
	 * 根据角色ID及名字查找角色
	 * 
	 * @param id
	 * @param name
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public SysRole findSysRoleByIdAndName(String id, String name, int wyId)
			throws FrameworkException {
		Criteria criteria = hibernateTemplate.getSessionFactory()
				.getCurrentSession().createCriteria(SysRole.class);
		if (!BaseUtil.isEmpty(name)) {
			criteria.add(Restrictions.eq("name", name));
		}
		if (!BaseUtil.isEmpty(id)) {
			criteria.add(Restrictions.ne("id", id));
		}
		if (!BaseUtil.isEmpty(wyId) && wyId != 0) {
			criteria.add(Restrictions.eq("wyId", wyId));
		}
		List<SysRole> sysRoles = criteria.list();
		if (!BaseUtil.isEmpty(sysRoles) && sysRoles.size() > 0)
			return sysRoles.get(0);
		return null;
	}

	@SuppressWarnings({"unchecked", "rawtypes"})
    public List findRoleByUserId(String id) {
		Session s = hibernateTemplate.getSessionFactory().openSession();
		SQLQuery sqlQuery = s
				.createSQLQuery("select s.sysroles_id from wyManage.KQ_SYSROLE_KQ_OPERATOR s where s.sysusers_opid=:userId");
		sqlQuery.setParameter("userId", id);
		List<String> roleIds = sqlQuery.list();
		return roleIds;
	}

}
